import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

public class User
{
	public static int userid = 0;
	public static String username = null;
	public static String password = null;
	public static String firstname = null;
	public static String lastname = null;
	public static String role = null;
	public static Boolean license = null;
	public static Boolean status = null;
	public static Date expired = null;
	public static String error = null;
	public static boolean login_status = false;
	public static boolean new_record = false;

	public User(String user, String pass, String fn, String ln, String ro, Boolean lic, Date ex)
	{
		new_record = true;
		username = user;
		password = pass;
		firstname = fn;
		lastname = ln;
		license = lic;
		role = ro;
		expired = ex;
	}

	public User(Boolean edit, String user, String pass, String fn, String ln, String ro, Boolean lic, Boolean st, Date ex) throws SQLException
	{
		new_record = false;
		Connector conn = new Connector("select * from user where username = '" + user + "'");
		ResultSet rs = conn.execute();
		if(rs.first())
		{
			userid = rs.getInt("userid");
		}
		rs.close();	
		status = st;
		username = user;
		password = pass;
		firstname = fn;
		lastname = ln;
		license = lic;
		role = ro;
		expired = ex;
	}

	public User(int uid) throws SQLException
	{
		new_record = false;
		userid = uid;

		Connector conn = new Connector("select * from user where userid = '" + userid + "'");
		ResultSet rs = conn.execute();
		if(rs.first())
		{
			username = rs.getString("username");
			password = rs.getString("password");
			firstname = rs.getString("firstname");
			lastname = rs.getString("lastname");
			role = rs.getString("role");
			license = rs.getBoolean("license");
			status = rs.getBoolean("status");
			expired = rs.getDate("expired");
		}
		rs.close();
	}
	
	public static boolean insert() throws SQLException
	{
		if(new_record)
		{
			Timestamp ex = null;
			if(expired != null)
			{
				ex = new Timestamp(expired.getTime());
			}

			String sql = "insert into user values(null,"
						+ "'" + firstname + "','"
						+ lastname + "','"
						+ username + "','"
						+ password + "',"
						+ license + ","
						+ "1,'"
						+ role + "',";
			if(ex != null)
			{
				sql	+= "'" + ex + "')";
			}
			else
			{
				sql	+= "null)";
			}

			Connector conn = new Connector(sql);
			conn.execute_sql();
			
			System.out.println("Create user complete.");
			return true;
		}
		else
			return false;
	}

	public static boolean update() throws SQLException
	{
		if(!new_record)
		{
			Timestamp ex = null;
			if(expired != null)
			{
				ex = new Timestamp(expired.getTime());
			}

			String sql = "update user set "
						+ "username='" + username + "',"
						+ "firstname='" + firstname + "',"
						+ "lastname='" + lastname + "',"
						+ "password='" + password + "',"
						+ "role='" + role + "',"
						+ "license=" + license + ","
						+ "status=" + status + ",";
			if(expired != null)
			{
				sql += "expired='" + ex + "'";
			}
			else
			{
				sql += "expired=null";
			}
						
				sql += " where userid='" + userid + "'";

			Connector conn = new Connector(sql);
			conn.execute_sql();
			
			return true;
		}
		else
			return false;
	}

	public static boolean delete() throws SQLException
	{
		if(!new_record)
		{
			String sql = "delete from user where userid='" + userid + "'";
			System.out.println(sql);

			Connector conn = new Connector(sql);
			conn.execute_sql();

			String sql1 = "delete from booking where userid='" + userid + "'";

			Connector conn1 = new Connector(sql);
			conn1.execute_sql();
			
			return true;
		}
		else
			return false;
	}

	public static void setPassword(String pass)
	{
		password = pass;
	}

	public static void setFirstName(String fn)
	{
		firstname = fn;
	}

	public static void setLastName(String ln)
	{
		lastname = ln;
	}

	public static void setRole(String ro)
	{
		role = ro;
	}

	public static void setLicense(Boolean lic)
	{
		status = lic;
	}

	public static void setStatus(Boolean st)
	{
		status = st;
	}

	public static void setExpired(Date ex)
	{
		expired = ex;
	}

	public static String getPassword()
	{
		return password;
	}

	public static String getFirstName()
	{
		return firstname;
	}

	public static String getLastName()
	{
		return lastname;
	}

	public static String getRole()
	{
		return role;
	}

	public static Boolean getLicense()
	{
		return license;
	}

	public static Boolean getStatus()
	{
		return status;
	}

	public static Date getExpired()
	{
		return expired;
	}
}
